package com.ruoyi.dmsIms.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DmsUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.http.DmsHttpParamUtils;
import com.ruoyi.common.utils.http.DmsHttpUtils;
import com.ruoyi.dmsIms.domain.InterOperation;
import com.ruoyi.dmsIms.domain.TestGlobalVar;
import com.ruoyi.dmsIms.domain.vo.DmsInter;
import com.ruoyi.dmsIms.domain.vo.InterDetail;
import com.ruoyi.dmsIms.domain.vo.SimpleInterList;
import com.ruoyi.dmsIms.mapper.InterOperationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dmsIms.mapper.ProInterfaceMapper;
import com.ruoyi.dmsIms.mapper.TestGlobalVarMapper;
import com.ruoyi.dmsIms.domain.ProInterface;
import com.ruoyi.dmsIms.service.IProInterfaceService;

/**
 * 项目接口Service业务层处理
 * 
 * @author ZhangSiRui
 * @date 2021-12-09
 */
@Service
public class ProInterfaceServiceImpl implements IProInterfaceService 
{
    @Autowired
    private ProInterfaceMapper proInterfaceMapper;

    @Autowired
    private TestGlobalVarMapper testGlobalVarMapper;

    @Autowired
    private InterOperationMapper interOperationMapper;

    /**
     * 查询项目接口
     * 
     * @param id 项目接口主键
     * @return 项目接口
     */
    @Override
    public ProInterface selectProInterfaceById(Long id)
    {
        return proInterfaceMapper.selectProInterfaceById(id);
    }

    /**
     * 查询项目接口列表
     * 
     * @param proInterface 项目接口
     * @return 项目接口
     */
    @Override
    public List<ProInterface> selectProInterfaceList(ProInterface proInterface)
    {
        return proInterfaceMapper.selectProInterfaceList(proInterface);
    }

    @Override
    public List<SimpleInterList> selectSimpleInterfaceList(String id) {
        return proInterfaceMapper.selectSimpleInterfaceList(id);
    }

    @Override
    public InterDetail selectDetailInter(Long id) {
        return proInterfaceMapper.selectDetailInter(id);
    }

    /**
     * 新增项目接口
     * 
     * @param proInterface 项目接口
     * @return 结果
     */
    @Override
    public int insertProInterface(ProInterface proInterface)
    {
        proInterface.setCreateTime(DateUtils.getNowDate());
        return proInterfaceMapper.insertProInterface(proInterface);
    }

    /**
     * 修改项目接口
     * 
     * @param proInterface 项目接口
     * @return 结果
     */
    @Override
    public int updateProInterface(ProInterface proInterface)
    {
        proInterface.setUpdateTime(DateUtils.getNowDate());
        return proInterfaceMapper.updateProInterface(proInterface);
    }

    /**
     * 批量删除项目接口
     * 
     * @param ids 需要删除的项目接口主键
     * @return 结果
     */
    @Override
    public int deleteProInterfaceByIds(Long[] ids)
    {
        return proInterfaceMapper.deleteProInterfaceByIds(ids);
    }

    /**
     * 删除项目接口信息
     * 
     * @param id 项目接口主键
     * @return 结果
     */
    @Override
    public int deleteProInterfaceById(Long id)
    {
        return proInterfaceMapper.deleteProInterfaceById(id);
    }

    @Override
    public Map<String, Object> executeInter(DmsInter dmsInter) {
        Map<String, Object> res = new HashMap<>();
        if (dmsInter.getMethod().equals("1")){
            if(!dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpGet(dmsInter.getUrl(), DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()));
            }else{
                res = DmsHttpUtils.sendHttpGet(dmsInter.getUrl());
            }
        }else if(dmsInter.getMethod().equals("2")){
            JSONObject jsonObject = JSON.parseObject(dmsInter.getBody());
            if(dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpPost(dmsInter.getUrl(),dmsInter.getBodyType(),jsonObject.getString("bodyData"));
            }else{
                res = DmsHttpUtils.sendHttpPost(dmsInter.getUrl(),dmsInter.getBodyType(),DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()),jsonObject.getString("bodyData"));
            }
        }else if(dmsInter.getMethod().equals("3")){
            JSONObject jsonObject = JSON.parseObject(dmsInter.getBody());
            if(dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpPut(dmsInter.getUrl(),jsonObject.getString("bodyData"));
            }else{
                res = DmsHttpUtils.sendHttpPut(dmsInter.getUrl(),DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()),jsonObject.getString("bodyData"));
            }
        }else if(dmsInter.getMethod().equals("4")){
            JSONObject jsonObject = JSON.parseObject(dmsInter.getBody());
            if(dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpPatch(dmsInter.getUrl(),jsonObject.getString("bodyData"));
            }else{
                res = DmsHttpUtils.sendHttpPatch(dmsInter.getUrl(),DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()),jsonObject.getString("bodyData"));
            }
        }else if(dmsInter.getMethod().equals("5")){
            JSONObject jsonObject = JSON.parseObject(dmsInter.getBody());
            if(dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpDel(dmsInter.getUrl(),jsonObject.getString("bodyData"));
            }else{
                res = DmsHttpUtils.sendHttpDel(dmsInter.getUrl(),DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()),jsonObject.getString("bodyData"));
            }
        }else{
            res.put("errorMsg","该请求方法暂时还未实现");
        }
        String username = SecurityUtils.getUsername();
            Long interId = dmsInter.getId();
            int interRes = 0;
            if(!res.containsKey("errorMsg")){
                interRes = 0;
            }else {
                interRes = 1;
        }
        InterOperation interOperation = new InterOperation();
        interOperation.setInterId(interId);
        interOperation.setInterRes(interRes);
        interOperation.setUserName(username);
        interOperation.setCreateTime(new Date());
        interOperationMapper.insertInterOperation(interOperation);
        return res;
    }

    @Override
    public Map<String, Object> executeInterPro(Long id) {
        Map<String, Object> res = null;
        ProInterface proInterface = proInterfaceMapper.selectProInterfaceById(id);
        DmsInter dmsInter = new DmsInter();
        dmsInter.setUrl(proInterface.getInterPath());
        try{
            JSONObject jsonObject = JSON.parseObject(proInterface.getInterParamBody());
            dmsInter.setBodyType(jsonObject.getString("bodyType"));
            dmsInter.setBody(proInterface.getInterParamBody());
        }catch (Exception e){
            dmsInter.setBodyType("none");
            dmsInter.setBody("{\"bodyType\":\"none\",\"bodyData\":\"\"}");
//            throw new ArrayIndexOutOfBoundsException("body信息填写错误");
        }
        dmsInter.setHeader(proInterface.getInterParamHeader());
        dmsInter.setMethod(proInterface.getInterType());
        List<Map<String,String>> tempValue = DmsUtils.matchValue(dmsInter.getUrl());
        if(tempValue.size() > 0){
            for (Map<String,String> value:
                    tempValue) {
                for (Map.Entry<String,String> entry:
                        value.entrySet()) {
                    TestGlobalVar testGlobalVar = testGlobalVarMapper.selectTestGlobalVarByName(entry.getValue());
                    if (testGlobalVar != null){
                        dmsInter.setUrl(dmsInter.getUrl().replace(entry.getKey(),testGlobalVar.getVarValue()));
                    }
                }
            }
        }
        List<Map<String,String>> tempValue1 = DmsUtils.matchValue(dmsInter.getHeader());
        if(tempValue1.size() > 0) {
            for (Map<String, String> value :
                    tempValue1) {
                for (Map.Entry<String, String> entry :
                        value.entrySet()) {
                    TestGlobalVar testGlobalVar = testGlobalVarMapper.selectTestGlobalVarByName(entry.getValue());
                    if (testGlobalVar != null) {
                        dmsInter.setHeader(dmsInter.getHeader().replace(entry.getKey(), testGlobalVar.getVarValue()));
                    }
                }
            }
        }
        List<Map<String,String>> tempValue2 = DmsUtils.matchValue(dmsInter.getBody());
        if(tempValue2.size() > 0){
            for (Map<String,String> value:
                    tempValue2) {
                for (Map.Entry<String,String> entry:
                        value.entrySet()) {
                    TestGlobalVar testGlobalVar = testGlobalVarMapper.selectTestGlobalVarByName(entry.getValue());
                    if (testGlobalVar != null){
                        dmsInter.setBody(dmsInter.getBody().replace(entry.getKey(),testGlobalVar.getVarValue()));
                    }
                }
            }
        }
        dmsInter.setId(proInterface.getId());
        if (dmsInter.getMethod().equals("1")){
            if(!dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpGet(dmsInter.getUrl(), DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()));
            }else{
                res = DmsHttpUtils.sendHttpGet(dmsInter.getUrl());
            }
        }else if(dmsInter.getMethod().equals("2")){
            JSONObject jsonObject = JSON.parseObject(dmsInter.getBody());
            if(dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpPost(dmsInter.getUrl(),dmsInter.getBodyType(),jsonObject.getString("bodyData"));
            }else{
                res = DmsHttpUtils.sendHttpPost(dmsInter.getUrl(),dmsInter.getBodyType(),DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()),jsonObject.getString("bodyData"));
            }
        }else if(dmsInter.getMethod().equals("3")){
            JSONObject jsonObject = JSON.parseObject(dmsInter.getBody());
            if(dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpPut(dmsInter.getUrl(),jsonObject.getString("bodyData"));
            }else{
                res = DmsHttpUtils.sendHttpPut(dmsInter.getUrl(),DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()),jsonObject.getString("bodyData"));
            }
        }else if(dmsInter.getMethod().equals("4")){
            JSONObject jsonObject = JSON.parseObject(dmsInter.getBody());
            if(dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpPatch(dmsInter.getUrl(),jsonObject.getString("bodyData"));
            }else{
                res = DmsHttpUtils.sendHttpPatch(dmsInter.getUrl(),DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()),jsonObject.getString("bodyData"));
            }
        }else if(dmsInter.getMethod().equals("5")){
            JSONObject jsonObject = JSON.parseObject(dmsInter.getBody());
            if(dmsInter.getHeader().isEmpty()){
                res = DmsHttpUtils.sendHttpDel(dmsInter.getUrl(),jsonObject.getString("bodyData"));
            }else{
                res = DmsHttpUtils.sendHttpDel(dmsInter.getUrl(),DmsHttpParamUtils.segmentationHeader(dmsInter.getHeader()),jsonObject.getString("bodyData"));
            }
        }else{
            res.put("errorMsg","该请求方法暂时还未实现");
        }
        String username = SecurityUtils.getUsername();
        Long interId = dmsInter.getId();
        int interRes = 0;
        if(!res.containsKey("errorMsg")){
            interRes = 0;
        }else {
            interRes = 1;
        }
        InterOperation interOperation = new InterOperation();
        interOperation.setInterId(interId);
        interOperation.setInterRes(interRes);
        interOperation.setUserName(username);
        interOperation.setCreateTime(new Date());
        interOperationMapper.insertInterOperation(interOperation);
        return res;
    }
}
